Skip to content

fix(Helpers): escape src and attribute values in img() and script_tag() - #10832

Open
banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/html-helper-escape-img-script-tag
Open

banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/html-helper-escape-img-script-tag

Conversation

@banghamid

@banghamid banghamid commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Description
Fixes #10813

In html_helper:

  • img() escaped its third $attributes parameter via stringify_attributes(), but concatenated values from the $src array (such as alt or other attributes) and relative src paths unescaped.
  • script_tag() concatenated both src and attribute values unescaped, unlike link_tag() and HTML media helpers which escape attribute values with esc().

This PR:

  • Escapes src and attribute values in img() using esc().
  • Escapes src and non-null attribute values in script_tag() using esc() while preserving null boolean attributes (e.g. async).
  • Adds regression tests in tests/system/Helpers/HTMLHelperTest.php.
  • Documents the escaping behavior change in docs/helpers/html_helper.md, docs/installation/upgrade_477.md, and docs/changelogs/v4.7.7.md.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 27, 2026

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add tests for absolute src URLs and document the behavior change for values that are already HTML-escaped?

@carson-codeigniter4 carson-codeigniter4 Bot added the stale Pull requests with conflicts label Dec 3, 2026
@carson-codeigniter4

This comment has been minimized.

Escape src and non-null attribute values in img() and script_tag() using esc() to prevent malformed HTML and attribute injection, consistent with link_tag() and stringify_attributes().
@banghamid
banghamid force-pushed the fix/html-helper-escape-img-script-tag branch from b1ccd3c to f67dac0 Compare December 3, 2026 03:38
@banghamid

Copy link
Copy Markdown
Contributor Author

@michalsn Thanks for the review! I have updated the PR to address your feedback and synced with \develop:

  • Added regression tests for absolute \src\ URLs (both string and array forms, including query parameters) in \ ests/system/Helpers/HTMLHelperTest.php.
  • Documented the escaping behavior and double-escaping note for already HTML-escaped values in \docs/helpers/html_helper.md\ and \docs/installation/upgrade_477.md.
  • Rebased onto \develop\ to resolve the changelog conflict in \docs/changelogs/v4.7.7.md.

@carson-codeigniter4 carson-codeigniter4 Bot removed the stale Pull requests with conflicts label Dec 3, 2026
@banghamid
banghamid requested a review from michalsn December 3, 2026 03:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: img() and script_tag() insert array values into the tag unescaped

2 participants